// 封装axios
// 实例化  请求拦截器 响应拦截器
import { getToken } from './token'

import axios from 'axios'
const http = axios.create({
    baseURL: 'http://geek.itheima.net/v1_0',
    timeout: 5000
})
// 添加请求拦截器
http.interceptors.request.use((config) => {

    // const token = getToken()
    if (getToken()) {
        config.headers.Authorization = `Bearer ${getToken()}`
    }

    return config
}, (error) => {
    return Promise.reject(error)
})

// 添加响应拦截器
http.interceptors.response.use((response) => {
    return response.data
}, (error) => {
    if (error.response.status === 401) {
        window.location.href = '/login'
    }
    return Promise.reject(error)
})

export { http }